/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2012-11-10
 */
public class IDataSet4iBatisImpl implements IDataSet4iBatis {

    private SqlMapClientTemplate sqlMapClientTemplate;

    private static final int BATCH_SIZE = 500;

    @SuppressWarnings("all")
    public List<BaseModel> queryAsList(final BaseModel baseModel) {
        return sqlMapClientTemplate.queryForList(baseModel.getStatmentId(), baseModel);
    }

    public BaseModel queryAsObject(final BaseModel baseModel) {
        return (BaseModel) sqlMapClientTemplate.queryForObject(baseModel.getStatmentId(), baseModel);
    }

    public boolean insert(final BaseModel baseModel) {
        sqlMapClientTemplate.insert(baseModel.getStatmentId(), baseModel);
        return true;
    }

    public boolean delete(final BaseModel baseModel) {
        sqlMapClientTemplate.delete(baseModel.getStatmentId(), baseModel);
        return true;
    }

    public boolean update(final BaseModel baseModel) {
        sqlMapClientTemplate.update(baseModel.getStatmentId(), baseModel);
        return true;
    }

    public boolean batchInsert(final List<BaseModel> baseModelList) {
        return sqlMapClientTemplate.execute(new SqlMapClientCallback<Boolean>() {
            public Boolean doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                executor.startBatch();
                for (int i = 0; i < baseModelList.size(); i++) {
                    BaseModel baseModel = baseModelList.get(i);
                    executor.insert(baseModel.getStatmentId(), baseModel);
                    if ((i + 1) % BATCH_SIZE == 0) {
                        executor.executeBatch();
                    }
                }
                executor.executeBatch();
                return true;
            }
        });
    }

    public boolean batchDelete(final List<BaseModel> baseModelList) {
        return sqlMapClientTemplate.execute(new SqlMapClientCallback<Boolean>() {
            public Boolean doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                executor.startBatch();
                for (int i = 0; i < baseModelList.size(); i++) {
                    BaseModel baseModel = baseModelList.get(i);
                    executor.delete(baseModel.getStatmentId(), baseModel);
                    if ((i + 1) % BATCH_SIZE == 0) {
                        executor.executeBatch();
                    }
                }
                executor.executeBatch();
                return true;
            }
        });
    }

    public boolean batchUpdate(final List<BaseModel> baseModelList) {
        return sqlMapClientTemplate.execute(new SqlMapClientCallback<Boolean>() {
            public Boolean doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                executor.startBatch();
                for (int i = 0; i < baseModelList.size(); i++) {
                    BaseModel baseModel = baseModelList.get(i);
                    executor.update(baseModel.getStatmentId(), baseModel);
                    if ((i + 1) % BATCH_SIZE == 0) {
                        executor.executeBatch();
                    }
                }
                executor.executeBatch();
                return true;
            }
        });
    }

    public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
        this.sqlMapClientTemplate = sqlMapClientTemplate;
    }

}
